<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/17 0017
 * Time: 1:51
 */

namespace app\Lib\Ioc\User;


use app\Lib\Exception\BlException;
use app\Lib\Model\Role\GetRoleUser;
use app\Lib\Model\Role\Role;
use app\Lib\Model\User\AddUserBase;
use app\Lib\Model\User\EditUser;
use app\Lib\Model\User\GetUser;
use app\Lib\Model\User\GetUserInfo;
use app\Lib\Model\User\User;
use app\validate\UserBaseValidate;
use think\Db;
use think\Session;

class UserBase
{
    private $validate;

    /**
     * 添加后台用户
     * @param $data
     * @return mixed
     */
    public function addAdminUserBase($data){
        return Db::transaction(function ()use($data){
            $validate = $this->getValidate();
            if(!$validate->scene('checkAdminAddData')->check($data)){
                throw new BlException($validate->getError());
            }
            $model = (new AddUserBase());
            $model->setAccount($data['account'])
                ->setBirthday($data['birthday'])
                ->setMobilephone($data['mobilephone'])
                ->setNickname($data['nickname'])
                ->setRegIp(session('opIp'))
                ->setRole(new Role($data['role_id']))
                ->setSex($data['sex'])
                ->setOpUser(new User($data['opUser']))
                ->setWeixin($data['weixin'])
                ->setAuth($data['auth']);
            $model->addAdminAccount();
            return true;
        });
    }

    public function saveUser($data){
        return Db::transaction(function ()use($data){
            $validate = $this->getValidate();
            if(!$validate->scene('checkAdminAddData')->check($data)){
                throw new BlException($validate->getError());
            }
            $model = (new EditUser());
            $model->setAccount($data['account'])
                ->setBirthday($data['birthday'])
                ->setMobilephone($data['mobilephone'])
                ->setNickname($data['nickname'])
                ->setRegIp(session('opIp'))
                ->setRole(new Role($data['role_id']))
                ->setSex($data['sex'])
                ->setOpUser(new User($data['opUser']))
                ->setWeixin($data['weixin'])
                ->setAccountStatus($data['account_status'])
                ->setAuth($data['auth']);
            if(!empty($data['password'])){
                $model->setPassword($data['password']);
            }
            $model->setUser(new User($data['id']));
            $model->run();
            return true;
        });
    }

    public function getValidate(){
        if(empty($this->validate)) {
            $this->validate = new UserBaseValidate();
        }
        return $this->validate;
    }

    /**
     * 验证微信用户是否存在
     * @param $data
     * @return bool|mixed
     */
    public function checkWeiXinUser($data){
        return  User::getWeiXinUid($data['openId']);
    }


    /**
     * 添加微信用户
     * @param $data
     * @return bool
     * @throws BlException
     */
    public function addWeiXinUser($data){
        $model = new AddUserBase();
        $model->setMobilephone($data['tel']);
        $model->setNickname($data['userName']);
        $model->setBirthday(strtotime($data['birthday']));
        $model->setSex($data['sex']);
        $model->setWeixin($data['openId']);
        $model->setRegIp(session('opIp'));
        return $model->addWebAccount();

    }

    public function getCount($data){
        $model = new GetUser();
        if(!empty($data['keyword']) && !empty($data['keywordType'])){
            $model->setKeywordType($data['keywordType']);
            $model->setKeyword($data['keyword']);
        }
        $model->setIsCount(true);
        return $model->run();
    }


    public function getList($data){
        $model = new GetUser();
        if(!empty($data['keyword']) && !empty($data['keywordType'])){
            $model->setKeywordType($data['keywordType']);
            $model->setKeyword($data['keyword']);
        }
        $model->setPage($data['page']);
        $model->setLimit($data['limit']);
        return $model->run();
    }

    public function getInfo($data){
        return (new GetUserInfo(new User($data['id'])))->run();
    }


    public function getDesignUser(){
        $model =  new GetRoleUser();
        return $model->getDeign();
    }

    public function getSupervisor(){
        $model =  new GetRoleUser();
        return $model->getSupervisor();
    }
}